﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLVatTu.Class;

namespace QLVatTu.DanhMuc
{
    public partial class frmChucVu : Form
    {
        public frmChucVu()
        {
            InitializeComponent();
        }

        KetNoiDB cn = new KetNoiDB();
        string tag = "";

        private void load_button(string tag)
        {
            if (tag == "them")
            {
                btnThem.Enabled = false;
                btnSua.Enabled = false;
                btnXoa.Enabled = false;

                btnLuu.Enabled = true;
                btnHuy.Enabled = true;

                txtMaChucVu.Enabled = false;
                txtTenChucVu.Text = "";
                txtTenChucVu.Enabled = true;
                txtTenChucVu.Text = "";
                txtGhiChu.Enabled = true;


            }
            else if (tag == "sua")
            {
                btnThem.Enabled = false;
                btnSua.Enabled = false;
                btnXoa.Enabled = false;

                btnLuu.Enabled = true;
                btnHuy.Enabled = true;

                txtMaChucVu.Enabled = false;
                txtTenChucVu.Enabled = true;
                txtGhiChu.Enabled = true;

            }
            else
            {
                btnThem.Enabled = true;
                btnSua.Enabled = true;
                btnXoa.Enabled = true;

                btnLuu.Enabled = false;
                btnHuy.Enabled = false;
                txtMaChucVu.Enabled = false;
                txtTenChucVu.Enabled = false;
                txtGhiChu.Enabled = false;
            }
        }

        private void load_grid()
        {
            if (cn.KetNoi())
            {
                DataTable tbl = cn.getTable("select row_number() over(order by MaChucVu) as STT,* from tbl_ChucVu");
                dtgChucVu.DataSource = tbl;
            }
        }
        private void frmChucVu_Load(object sender, EventArgs e)
        {
            load_button(tag);
            load_grid();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            tag = "them";
            load_button(tag);
            txtMaChucVu.Text = cn.SinhMa("MaChucVu","tbl_ChucVu","CV");
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            tag = "sua";
            load_button(tag);
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            load_button("");
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            if (this.TopLevel == false) this.Parent.Parent.Controls.Remove(this.Parent);
            else this.Close();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (dtgChucVu.RowCount > 0)
            {
                DialogResult msg = MessageBox.Show("Bạn có muốn xóa hay không?","Thông báo",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
                if (msg == DialogResult.Yes)
                {
                    cn.Delete_Table("tbl_ChucVu","MaChucVu='"+txtMaChucVu.Text+"'");
                    load_button("");
                    load_grid();
                }
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (tag == "them")
            {
                if (txtTenChucVu.Text.ToString().Trim() == "")
                {
                    MessageBox.Show("Bạn chưa nhập tên chức vụ");
                    txtTenChucVu.Focus();
                    return;
                }
                string TenChucVu = cn.LayDuLieu("select TenChucVu from tbl_ChucVu where lower(TenChucVu)=N'"+txtTenChucVu.Text.ToLower()+"'");
                if (TenChucVu != "")
                {
                    MessageBox.Show("Tên chức vụ đã tồn tại","Thông báo",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    txtTenChucVu.Focus();
                    return;
                }
                cn.Insert_Table("tbl_ChucVu", "MaChucVu,TenChucVu,GhiChu", "'" + txtMaChucVu.Text + "',N'" + txtTenChucVu.Text + "',N'" + txtGhiChu.Text + "'");
            }
            else
            {
                cn.Update_Table("tbl_ChucVu","TenChucVu=N'"+txtTenChucVu.Text+"',GhiChu=N'"+txtGhiChu.Text+"'","MaChucVu='"+txtMaChucVu.Text+"'");
            }
            load_button("");
            load_grid();
        }

        private void dtgChucVu_SelectionChanged(object sender, EventArgs e)
        {
            if (dtgChucVu.RowCount > 0)
            {
                txtMaChucVu.Text = dtgChucVu.CurrentRow.Cells["MaChucVu"].Value.ToString();
                txtTenChucVu.Text = dtgChucVu.CurrentRow.Cells["TenChucVu"].Value.ToString();
                txtGhiChu.Text = dtgChucVu.CurrentRow.Cells["GhiChu"].Value.ToString();
            }
        }
    }
}
